function executeJs() {
    var deviceHandler = {
        params: {},
        data: {},
        access_token: '',
        resultData: [],
        groupArray: [],

        setParams: function (params) {
            if (typeof params !== 'object') {
                return;
            }
            this.params = params;
        },
        setData: function (data) {
            if (typeof data !== 'object') {
                return;
            }
            this.data = data;
        },

        fetchDeviceInfo: function () {
            var resultStr, requestUrl = this.params.deviceInfoUrl, request = $CurlHttpRequest;
            this.checkHttpProxy(request);
            this.checkHttps(requestUrl, request);

            resultStr = request.get(requestUrl);
            var result = JSON.parse(resultStr.match(/\[.*\]/)[0]);

            if (result) {
                this.resultData = result;
            } else {
                throw new Error('获取设备信息失败');
            }
        },

        checkHttps: function (requestUrl, request) {
            if (requestUrl.startsWith('https://')) {
                request.enableHttps();
            }
        },

        checkHttpProxy: function (request) {
            if (this.params.HTTPProxy) {
                request.setProxy(this.params.HTTPProxy);
            }
        },

        // 查找设备ID为3、4、1326的原设备
        getOriginalDevices: function (targetIds) {
            var filteredDevices = [];
            for (var i = 0; i < this.resultData.length; i++) {
                var device = this.resultData[i];
                if (targetIds.indexOf(device.id) !== -1) {
                    filteredDevices.push(device);
                }
            }
            return filteredDevices;
        },

        // 查找温湿度设备（SUBTYPE为17）
        getHumidityDevices: function (parentIds) {
            var humidityDevices = [];
            for (var i = 0; i < this.resultData.length; i++) {
                var device = this.resultData[i];
                if (device.SUBTYPE === "17" && parentIds.indexOf(device.ParentID) !== -1) {
                    humidityDevices.push(device);
                }
            }
            return humidityDevices;
        },

        // 将温湿度设备添加到原设备的children属性中
        addHumidityToDevices: function (originalDevices, humidityDevices) {
            for (var i = 0; i < originalDevices.length; i++) {
                var device = originalDevices[i];
                device.children = [];
                for (var j = 0; j < humidityDevices.length; j++) {
                    var humidityDevice = humidityDevices[j];
                    if (humidityDevice.ParentID === device.id) {
                        device.children.push(humidityDevice);
                    }
                }
            }
        },
        toGroupArray: function (originalDevices){
            var resultStr, requestUrl = this.params.ipUrL, request = $CurlHttpRequest ,dataArr = [];
            originalDevices.forEach(function (device){
                if (device.children.length > 0){
                    var ipUrl =  requestUrl + '?ip='+ device.children[0].IP;

                    $ScriptLog.Log(4, "ipUrl 地址 " +ipUrl);
                    resultStr =  request.get(ipUrl)
                    $ScriptLog.Log(5, "ipUrl 数据 " +resultStr);
                    var result = JSON.parse(resultStr.match(/\[.*\]/)[0])

                    if (result.length > 30) {
                        result = result.slice(0, 30);
                    }
                    var maxTemp = -Infinity;
                    var minTemp = Infinity;
                    var maxHumidity = -Infinity;
                    var minHumidity = Infinity;
                    result.forEach(function (item){
                        // 查找温度
                        if (item.subName.indexOf("温度") !== -1) {
                            var temp = parseFloat(item.value);
                            if (temp > maxTemp) {
                                maxTemp = temp;
                            }
                            if (temp < minTemp) {
                                minTemp = temp;
                            }
                        }

                        // 查找湿度
                        if (item.subName.indexOf("湿度") !== -1) {
                            var humidity = parseFloat(item.value);
                            if (humidity > maxHumidity) {
                                maxHumidity = humidity;
                            }
                            if (humidity < minHumidity) {
                                minHumidity = humidity;
                            }
                        }
                    })
                    $ScriptLog.Log(6, "最大最小" +maxTemp + " " + minTemp + " " + maxHumidity + " " + minHumidity);

                    var sss = {
                        title: device.Name + "温湿度",
                        rowCount:2,
                        layout:"row",
                        items: [{
                            itemName: "最高温度",
                            itemValue: maxTemp,
                            itemUnit: "℃",
                            iconClass: "temperature"
                        },
                            {
                                itemName: "最低温度",
                                itemValue: minTemp,
                                itemUnit: "℃",
                                iconClass: "temperature"
                            },
                            {
                                itemName: "最高湿度",
                                itemValue: maxHumidity,
                                itemUnit: "%",
                                iconClass: "temperature"
                            },
                            {
                                itemName: "最低湿度",
                                itemValue: minHumidity,
                                itemUnit: "%",
                                iconClass: "temperature"
                            }
                        ]
                    }
                    $ScriptLog.Log(7,"数据体"+ JSON.stringify(sss));
                    dataArr.push(sss);
                }
            })
            this.groupArray = dataArr;

        },

        execute: function () {
            try {
                var params = JSON.parse($Params), data = JSON.parse($Data);
                ['deviceInfoUrl'].forEach(function (field) {
                    if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
                        throw '请求参数未设置: "' + field + '".';
                    }
                });

                this.setParams(params);
                this.setData(data);
                this.fetchDeviceInfo();

                // 目标设备ID数组
                var targetIds = [3, 4, 1326];

                // 获取目标设备和对应的温湿度设备
                var originalDevices = this.getOriginalDevices(targetIds);
                var humidityDevices = this.getHumidityDevices(targetIds);

                // 将温湿度设备添加到原设备的children中
                this.addHumidityToDevices(originalDevices, humidityDevices);
                $ScriptLog.Log(3,  "　获得的主机数据　"+ JSON.stringify(originalDevices));
                this.toGroupArray(originalDevices)
                return  JSON.stringify(this.groupArray);

            } catch (error) {
                var message = '获取并更新设备信息失败: ' + error;
                $ScriptLog.Log(3, message);
                return $Result.error(message);
            }
        }
    };

    return deviceHandler.execute();
}

executeJs();
